package com.qixin.modules.rzInvoice.service.impl;

import com.qixin.modules.rzInvoice.entity.EaRzHkysdzkpxcd;
import com.qixin.modules.rzInvoice.entity.EaRzHkysdzkpxcdItems;
import com.qixin.modules.rzInvoice.mapper.EaRzHkysdzkpxcdItemsMapper;
import com.qixin.modules.rzInvoice.mapper.EaRzHkysdzkpxcdMapper;
import com.qixin.modules.rzInvoice.service.IEaRzHkysdzkpxcdService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 航空运输电子客票行程单
 * @Author: qixin-boot
 * @Date:   2021-07-08
 * @Version: V1.0
 */
@Service
public class EaRzHkysdzkpxcdServiceImpl extends ServiceImpl<EaRzHkysdzkpxcdMapper, EaRzHkysdzkpxcd> implements IEaRzHkysdzkpxcdService {

	@Autowired
	private EaRzHkysdzkpxcdMapper eaRzHkysdzkpxcdMapper;
	@Autowired
	private EaRzHkysdzkpxcdItemsMapper eaRzHkysdzkpxcdItemsMapper;
	
	@Override
	@Transactional
	public void saveMain(EaRzHkysdzkpxcd eaRzHkysdzkpxcd, List<EaRzHkysdzkpxcdItems> eaRzHkysdzkpxcdItemsList) {
		eaRzHkysdzkpxcdMapper.insert(eaRzHkysdzkpxcd);
		if(eaRzHkysdzkpxcdItemsList!=null && eaRzHkysdzkpxcdItemsList.size()>0) {
			for(EaRzHkysdzkpxcdItems entity:eaRzHkysdzkpxcdItemsList) {
				//外键设置
				entity.setRzHkysdzkpxcdId(eaRzHkysdzkpxcd.getId());
				eaRzHkysdzkpxcdItemsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(EaRzHkysdzkpxcd eaRzHkysdzkpxcd,List<EaRzHkysdzkpxcdItems> eaRzHkysdzkpxcdItemsList) {
		eaRzHkysdzkpxcdMapper.updateById(eaRzHkysdzkpxcd);
		
		//1.先删除子表数据
		eaRzHkysdzkpxcdItemsMapper.deleteByMainId(eaRzHkysdzkpxcd.getId());
		
		//2.子表数据重新插入
		if(eaRzHkysdzkpxcdItemsList!=null && eaRzHkysdzkpxcdItemsList.size()>0) {
			for(EaRzHkysdzkpxcdItems entity:eaRzHkysdzkpxcdItemsList) {
				//外键设置
				entity.setRzHkysdzkpxcdId(eaRzHkysdzkpxcd.getId());
				eaRzHkysdzkpxcdItemsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		eaRzHkysdzkpxcdItemsMapper.deleteByMainId(id);
		eaRzHkysdzkpxcdMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			eaRzHkysdzkpxcdItemsMapper.deleteByMainId(id.toString());
			eaRzHkysdzkpxcdMapper.deleteById(id);
		}
	}
	
}
